package fr.miage.orleans.projetindus2014;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import fr.miage.orleans.projetindus2014.adapter.GridViewImageAdapter;
import fr.miage.orleans.projetindus2014.helper.AppConstant;
import fr.miage.orleans.projetindus2014.helper.Utils;

public class DetailsActivty extends Activity {

	private TextView title = null;
	private TextView d = null;
	private TextView d2 = null;
	private TextView d3 = null;
	private ImageView iView = null;
	private ImageView splash = null;
	final String EXTRA_DETAILS = "details";

	//
	private Utils utils;
	private ArrayList<String> imagePaths = new ArrayList<String>();
	private GridViewImageAdapter adapter;
	private GridView gridView;
	private int columnWidth;

	//

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.details);
		// ///
		gridView = (GridView) findViewById(R.id.grid_view);

		utils = new Utils(this);

		// Initilizing Grid View
		InitilizeGridLayout();

		// loading all image paths from SD card
		imagePaths = utils.getFilePaths();

		// Gridview adapter
		adapter = new GridViewImageAdapter(DetailsActivty.this, imagePaths,
				columnWidth);

		// setting grid view adapter
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView parent, View v, int position,
					long id) {

				// Toast.makeText(GridViewActivity.this, "" + position,
				// Toast.LENGTH_SHORT).show();

				Intent i = new Intent(DetailsActivty.this,
						FullScreenViewActivity.class);

				i.putExtra("position", position);

				DetailsActivty.this.startActivity(i);

			}

		});

		// //////
		title = (TextView) findViewById(R.id.tDet);
		d = (TextView) findViewById(R.id.dDet);
		d2 = (TextView) findViewById(R.id.dDet2);
		d3 = (TextView) findViewById(R.id.dDet3);
		/*
		 * iView = (ImageView) findViewById(R.id.iDet);
		 * iView.setOnTouchListener(new View.OnTouchListener() {
		 * 
		 * @Override public boolean onTouch(View v, MotionEvent event) { Context
		 * context = getApplicationContext(); CharSequence text = "Tap"; int
		 * duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context,
		 * text, duration); toast.show(); return true;
		 * 
		 * } });
		 */
		Intent intent = getIntent();
		if (intent != null) {
			title.setText(intent.getStringExtra(EXTRA_DETAILS));
			d.setText(intent.getStringExtra(EXTRA_DETAILS));
			d2.setText(intent.getStringExtra(EXTRA_DETAILS));
			d3.setText(intent.getStringExtra(EXTRA_DETAILS));
		}
		// Show the Up button in the action bar.
		setupActionBar();
	}

	/**
	 * Set up the {@link android.app.ActionBar}.
	 */
	private void setupActionBar() {

		getActionBar().setDisplayHomeAsUpEnabled(true);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.details_activty, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private void InitilizeGridLayout() {
		Resources r = getResources();
		float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
				AppConstant.GRID_PADDING, r.getDisplayMetrics());

		columnWidth = (int) ((utils.getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);

		gridView.setNumColumns(AppConstant.NUM_OF_COLUMNS);
		gridView.setColumnWidth(columnWidth);
		gridView.setStretchMode(GridView.NO_STRETCH);
		gridView.setPadding((int) padding, (int) padding, (int) padding,
				(int) padding);
		gridView.setHorizontalSpacing((int) padding);
		gridView.setVerticalSpacing((int) padding);
	}

}
